package org.frankkie.parcdroidprj;

import android.app.Activity;
import android.content.Context;
import android.hardware.Sensor;
import android.hardware.SensorEvent;
import android.hardware.SensorEventListener;
import android.hardware.SensorManager;
import android.os.Bundle;
import java.util.List;

public class SensorTester extends Activity {

    public SensorManager mSensorManager;
    public SensorTesterDraw mView;
    public static float[] compassValues = new float[3];
    public static float[] tempValues = new float[3];
    public static float[] accelValues = new float[3];
    public static float[] magnetValues = new float[3];
    public static Sensor temp;
    public static Sensor compass;
    public static Sensor accel;
    public static Sensor magnet;
    public final SensorEventListener tempListener = new SensorEventListener() {

        public void onSensorChanged(SensorEvent arg1) {
            SensorEvent arg0 = arg1;
            if (arg0.sensor.getType() == temp.getType()) {
                System.out.print("sensorChanged-temp (");
                tempValues = arg0.values;
                for (int a = 0; a < arg0.values.length; a++) {
                    if (a != arg0.values.length - 1) {
                        System.out.print(arg0.values[a] + ",");
                    } else {
                        System.out.println(arg0.values[a] + ")");
                    }
                }
            } 

            if (mView != null) {
                mView.invalidate();
            }
            //throw new UnsupportedOperationException("Not supported yet.");
            arg0 = null;
            arg1 = null;
        }

        public void onAccuracyChanged(Sensor arg0, int arg1) {
            //throw new UnsupportedOperationException("Not supported yet.");
        }
    };
    public final SensorEventListener compassListener = new SensorEventListener() {

        public void onSensorChanged(SensorEvent arg1) {
            SensorEvent arg0 = arg1;
            if (arg0.sensor.getType() == compass.getType()) {
                System.out.println("sensorChanged-compass (" + arg0.values[0] + ", " + arg0.values[1] + ", " + arg0.values[2] + ")");
                compassValues = arg0.values;
            }

            if (mView != null) {
                mView.invalidate();
            }
            //throw new UnsupportedOperationException("Not supported yet.");
            arg0 = null;
            arg1 = null;
        }

        public void onAccuracyChanged(Sensor arg0, int arg1) {
            //throw new UnsupportedOperationException("Not supported yet.");
        }
    };

    public final SensorEventListener accelListener = new SensorEventListener() {

        public void onSensorChanged(SensorEvent arg1) {
            SensorEvent arg0 = arg1;
            if (arg0.sensor.getType() == accel.getType()) {
                System.out.print("sensorChanged-accel (");
                accelValues = arg0.values;
                for (int a = 0; a < arg0.values.length; a++) {
                    if (a != arg0.values.length - 1) {
                        System.out.print(arg0.values[a] + ",");
                    } else {
                        System.out.println(arg0.values[a] + ")");
                    }
                }
            }
            if (mView != null) {
                mView.invalidate();
            }
            //throw new UnsupportedOperationException("Not supported yet.");
            arg0 = null;
            arg1 = null;
        }

        public void onAccuracyChanged(Sensor arg0, int arg1) {
            //throw new UnsupportedOperationException("Not supported yet.");
        }
    };

    public final SensorEventListener magnetListener = new SensorEventListener() {

        public void onSensorChanged(SensorEvent arg1) {
            SensorEvent arg0 = arg1;
            if (arg0.sensor.getType() == magnet.getType()) {
                System.out.print("sensorChanged-magnet (");
                magnetValues = arg0.values;
                for (int a = 0; a < arg0.values.length; a++) {
                    if (a != arg0.values.length - 1) {
                        System.out.print(arg0.values[a] + ",");
                    } else {
                        System.out.println(arg0.values[a] + ")");
                    }
                }
            }

            if (mView != null) {
                mView.invalidate();
            }
            //throw new UnsupportedOperationException("Not supported yet.");
            arg0 = null;
            arg1 = null;
        }

        public void onAccuracyChanged(Sensor arg0, int arg1) {
            //throw new UnsupportedOperationException("Not supported yet.");
        }
    };

    @Override
    protected void onCreate(Bundle icicle) {
        super.onCreate(icicle);
        mSensorManager = (SensorManager) getSystemService(Context.SENSOR_SERVICE);

        List<Sensor> sensorLijst = mSensorManager.getSensorList(Sensor.TYPE_ALL);
        for (int a = 0; a < sensorLijst.size(); a++) {
            Sensor deA = sensorLijst.get(a);
            System.out.println("Sensor: name=" + deA.getName() + "&vendor=" + deA.getVendor() + "&type=" + deA.getType() + "\n" +
                    "&maxRange=" + deA.getMaximumRange() + "&power=" + deA.getPower() + "&resolution=" + deA.getResolution() + "&version=" + deA.getVersion());
        }

        //Sensors:
        compass = mSensorManager.getDefaultSensor(Sensor.TYPE_ORIENTATION);
        temp = mSensorManager.getDefaultSensor(Sensor.TYPE_TEMPERATURE);
        accel = mSensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER);
        magnet = mSensorManager.getDefaultSensor(Sensor.TYPE_MAGNETIC_FIELD);

        mView = new SensorTesterDraw(this);
        setContentView(mView);
    }

    @Override
    protected void onResume() {
        System.out.println("onResume");
        super.onResume();

        //COMPASS
        mSensorManager.registerListener(compassListener, compass, SensorManager.SENSOR_DELAY_NORMAL);
        //TEMP
        mSensorManager.registerListener(tempListener, temp, SensorManager.SENSOR_DELAY_NORMAL);
        //Accel
        mSensorManager.registerListener(accelListener, accel, SensorManager.SENSOR_DELAY_NORMAL);
        //Magnet
        mSensorManager.registerListener(magnetListener, magnet, SensorManager.SENSOR_DELAY_NORMAL);
        //registerListener(mListener,
        //	SensorManager.SENSOR_ORIENTATION,
        //	SensorManager.SENSOR_DELAY_GAME);
    }

    @Override
    protected void onStop() {
        System.out.println("onStop");
        mSensorManager.unregisterListener(tempListener);
        mSensorManager.unregisterListener(compassListener);
        mSensorManager.unregisterListener(magnetListener);
        mSensorManager.unregisterListener(accelListener);
        super.onStop();
    }
}
